/**
 * CircleBillListActivity.java
 * com.jamin.aabill.ui
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-28 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jamin.aabill.R;
import com.jamin.aabill.data.Bill;
import com.jamin.aabill.manager.BillManager;
import com.jamin.aabill.utils.AppConstant;
import com.jamin.aabill.utils.TimeUtils;
import com.jamin.aabill.utils.Utils;

/**
 * ClassName:CircleBillListActivity
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-8-28 上午10:39:21 
 */
public class CircleBillListActivity extends Activity{

	private static final String TAG = "CircleBillListActivity ====wangjieming ";

	private static final int MENU_DELETE_BILL = 0;
	//	private static final int MENU_MODIFY_BILL = 1;

	private static final int DIALOG_DELETE_BILL = 0;

	BroadcastReceiver receiver = null;

	List<Bill> billList = new ArrayList<Bill>();
	ListView mBillListView = null;
	BillListAdapter mBillListAdpater = null;
	long circleId = -1;
	long billId = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.circle_bill_list_activity);
		circleId = getIntent().getLongExtra("circleId", -1);
		initView();
		initData();
		receiver = new RefreshBroadcastReceiver();
		registerReceiver(receiver, new IntentFilter(AppConstant.BILL_DETAIL_REFRESH_ACTION));
	}

	/**
	 * 刷新View<br>
	 * @method refreshView
	 * @throws 
	 * @since v1.0
	 */
	private void refreshView(){
		billList = AABillApplication.mBillManager.getBillList(circleId);
		mBillListAdpater.notifyAdapter(billList);
		mBillListAdpater.notifyDataSetChanged();

	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);
		Utils.log(TAG, "CircleBillListActivity=========onDestroy");
	}







	private void initView() {
		mBillListView = (ListView) findViewById(R.id.circle_bill_list_view);
		mBillListView.setOnCreateContextMenuListener(this);
		mBillListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
				Intent intent = new Intent(CircleBillListActivity.this , BillDetailActivity.class);
				intent.putExtra("billId", billList.get(position).getBILL_ID());
				startActivity(intent);
			}
		});
	}

	private void initData() {
		billList = AABillApplication.mBillManager.getBillList(circleId);
		mBillListAdpater = new BillListAdapter(this, billList);
		mBillListView.setAdapter(mBillListAdpater);
	}


	//列表长按菜单
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
		billId = Long.parseLong(((TextView) menuInfo.targetView.findViewById(R.id.bill_list_item_id)).getText().toString());
		Utils.log(TAG, "billId = " + billId);
		Utils.log(TAG, "item.getItemId() = " + item.getItemId());
		switch(item.getItemId()){
		case MENU_DELETE_BILL:
			showDialog(DIALOG_DELETE_BILL);
			break;
			//		case MENU_MODIFY_BILL:
			//			Intent intent = new Intent(CircleBillListActivity.this , AddCircleBillActivity.class);
			//			intent.putExtra("billId", billId);
			//			startActivity(intent);
			//			break;
			//		case MENU_CLOSE_CIRCLE:
			//			CircleDao mCircleDao = new CircleDao();
			//			
			//			break;
		}
		return super.onContextItemSelected(item);
	}



	@Override
	public void onCreateContextMenu(ContextMenu menu , View view ,ContextMenuInfo menuInfo ) {
		super.onCreateContextMenu(menu , view , menuInfo);
		menu.setHeaderTitle(getString(R.string.aa_bill_menu_bill_title));     
		menu.add(0, MENU_DELETE_BILL , 0 , getString(R.string.aa_bill_menu_delete_bill) );  
		//		menu.add(0, MENU_MODIFY_BILL , 0 , getString(R.string.aa_bill_menu_modify_bill) );  
		//		menu.add(0, MENU_CLOSE_CIRCLE , 0 , getString(R.string.aa_bill_menu_close_circle) );  
	}


	@Override
	protected Dialog onCreateDialog(int id) {

		switch(id){
		case DIALOG_DELETE_BILL:
			Dialog dialog = new AlertDialog.Builder(this)
			.setTitle(getString(R.string.aa_bill_dialog_delete_bill_title))
			.setMessage(getString(R.string.aa_bill_dialog_delete_bill_msg))
			.setPositiveButton(getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					BillManager manager = new BillManager();
					manager.deleteBillListFromCircle(billId);
					//刷新账单列表
					refreshView();
				}
			})
			.setNegativeButton(getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			}).create();
			dialog.show();
			return dialog;

		}

		return super.onCreateDialog(id);
	}


	class BillListAdapter extends BaseAdapter{

		LayoutInflater inflater = null;
		List<Bill> mBillList = null;


		public BillListAdapter(Context ctx , List<Bill> mBillList){
			this.mBillList = mBillList;
			inflater = LayoutInflater.from(ctx);
		}

		public void notifyAdapter(List<Bill> mBillList){
			this.mBillList = mBillList;
		}


		@Override
		public int getCount() {
			return mBillList.size();

		}

		@Override
		public Object getItem(int position) {
			return mBillList.get(position);

		}

		@Override
		public long getItemId(int position) {
			return position;

		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			if(convertView == null){
				convertView = inflater.inflate(R.layout.circle_bill_list_item, null);
				viewHolder = new ViewHolder();
				viewHolder.mIdView = (TextView) convertView.findViewById(R.id.bill_list_item_id);
				viewHolder.mProjectView = (TextView) convertView.findViewById(R.id.bill_list_item_project);
				viewHolder.mCostView = (TextView) convertView.findViewById(R.id.bill_list_item_cost);
				viewHolder.mCreateTimeView = (TextView) convertView.findViewById(R.id.bill_list_item_create_time);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.mIdView.setText(mBillList.get(position).getBILL_ID() + "");
			viewHolder.mProjectView.setText(mBillList.get(position).getPROJECT());
			viewHolder.mCostView.setText(getString(R.string.circle_bill_cost, mBillList.get(position).getCOST()));
			viewHolder.mCreateTimeView.setText(TimeUtils.FormatTimeForm(mBillList.get(position).getCREATE_AT() , "MM-dd"));
			return convertView;

		}


		class ViewHolder{
			TextView mIdView = null;
			TextView mProjectView = null;
			TextView mCostView = null;
			TextView mCreateTimeView = null;
		}
	}


	class RefreshBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context ctx, Intent intent) {
			if(intent.getAction().equals(AppConstant.BILL_DETAIL_REFRESH_ACTION)){
				refreshView();
			}
		}

	}



}

